package cp2013;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

/**
 *
 * @author AcerXPS
 */
public final class ItemPanel extends JPanel{

    private JTextArea lblName;
    private JTextArea lblDate;
    
    private JTextArea lblDesc;
    private JTextArea lblStartBid;
    private JTextArea lblCurrentBid;
    private JTextArea lblSeller;
    private JTextArea lblHighestBidder;
    private JButton btnBid;
    private Item item;
    private static final int MAX_WIDTH = 600;
    private static final int MAX_HEIGHT = 200;
    private static final int MIN_WIDTH = 600;
    private static final int MIN_HEIGHT = 120;

    public ItemPanel(Item i){
        item = i;
        this.initateObjectPanel();
    }
    public Item getItem(){
        return item;
    }
    
    public void initateObjectPanel(){
        setLayout(new GridLayout(2,4));
        setBackground(Color.white);
        Dimension maxD = new Dimension(MAX_WIDTH, MAX_HEIGHT);
        Dimension minD = new Dimension(MIN_WIDTH, MIN_HEIGHT);
        this.setMaximumSize(maxD);
        this.setMinimumSize(minD);
        setBorder(new LineBorder(Color.black));
        initiateLabels();

        add(lblName);
        add(lblDate);
        add(lblDesc);
        add(lblSeller);
        add(lblStartBid);
        add(lblCurrentBid);
        add(lblHighestBidder);
        add(btnBid);
    }//end of initiateObjectPanel()

    public void initiateLabels(){
        lblName = new JTextArea("> Item Name: \n " + item.getItemName());
        lblName.setLineWrap(true);
        lblName.setEditable(false);
        lblDate = new JTextArea ("> Finish Date: \n " + item.getFinishDate());
        lblDate.setLineWrap(true);
        lblDate.setEditable(false);
        lblDesc = new JTextArea("> Description: \n" +item.getDescription() +"\n");
        lblDesc.setLineWrap(true);
        lblDesc.setEditable(false);
        lblSeller = new JTextArea("> Seller Name: \n  " + item.getSellerName());
        lblSeller.setLineWrap(true);
        lblSeller.setEditable(false);
        lblStartBid = new JTextArea("> Starting Bid: \n " + insertCommas(String.valueOf(item.getStartingBid())) + " Token");
        lblStartBid.setLineWrap(true);
        lblStartBid.setEditable(false);
        lblCurrentBid = new JTextArea("> Current Bid: \n " + insertCommas(String.valueOf(item.getCurrentBid())) + " Token");
        lblCurrentBid.setLineWrap(true);
        lblCurrentBid.setEditable(false);
        lblHighestBidder = new JTextArea("> Highest Bidder: \n " + item.getHighestBidder());
        lblHighestBidder.setLineWrap(true);
        lblHighestBidder.setEditable(false);
        btnBid = new JButton ("Bid Now!");
    }//end of initiateLabels()

    public void addItemPanelListener(ActionListener listener){
        btnBid.addActionListener(listener);
    }//end of addListener()

    private String insertCommas(String str)
  {
    String newStr = str;
    int firstComma = str.indexOf(",");
    if(firstComma < 0) firstComma = str.length();
    if(str.length() > 3 && firstComma > 3)
    {
      newStr = insertCommas(new StringBuffer(str).insert(firstComma-3,",").toString());
    }
    return newStr;
  }

}
